package ai.people.netmon.telegram.controller;

//import ai.people.core.security.support.RequestHolderUtil;
import ai.people.netmon.framework.businessenum.AnalysisSourceEnum;
import ai.people.netmon.framework.businessenum.AnalysisTypeEnum;
import ai.people.netmon.framework.domain.telegram.entity.AnalysisDataUser;
import ai.people.netmon.framework.domain.telegram.vo.account.AnalyzedAccount;
import ai.people.netmon.framework.domain.telegram.vo.account.ChannelsMember;
import ai.people.netmon.framework.domain.telegram.vo.account.SimilarAccount;
import ai.people.netmon.framework.domain.telegram.vo.channels.ChatMessage;
import ai.people.netmon.framework.model.response.ListResponse;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.telegram.domain.request.AnalyzedListRequest;
import ai.people.netmon.telegram.domain.request.JoinAnalysisRequest;
import ai.people.netmon.telegram.domain.request.account.ChannelsMemberRequest;
import ai.people.netmon.telegram.domain.request.account.SimilarAccountRequest;
import ai.people.netmon.telegram.domain.request.account.SpeakMessageRequest;
import ai.people.netmon.telegram.service.AccountAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 账户分析控制器
 *
 * @author yuanqinglong
 * @date 2022/1/12 18:24
 */
@Validated
@RestController
@RequestMapping("/account/analysis")
public class AccountAnalysisController {

    @Autowired
    private AccountAnalysisService accountAnalysisServiceImpl;


    /**
     * 群组列表
     *
     * @param channelsMemberRequest 参数
     * @return {@link Result}<{@link PageResponse}<{@link ChannelsMember}>>
     */
    @PostMapping("/channelsList")
    public Result<PageResponse<ChannelsMember>> channelsList(@Valid @RequestBody ChannelsMemberRequest channelsMemberRequest) {
        PageResponse<ChannelsMember> channelsList = accountAnalysisServiceImpl.channelsList(channelsMemberRequest);
        return ResultUtil.success(channelsList);
    }


    /**
     * 发言消息列表
     *
     * @param speakMessageRequest 参数
     * @return {@link Result}<{@link PageResponse}<{@link ChatMessage}>>
     */
    @PostMapping("/speakMessages")
    public Result<PageResponse<ChatMessage>> speakMessageList(@Valid @RequestBody SpeakMessageRequest speakMessageRequest) {
        PageResponse<ChatMessage> speakMessageList = accountAnalysisServiceImpl.speakMessageList(speakMessageRequest);
        return ResultUtil.success(speakMessageList);
    }


    /**
     * 类似账户
     *
     * @param similarAccountRequest 参数
     * @return {@link Result}<{@link ListResponse}<{@link List}<{@link SimilarAccount}>>>
     */
    @PostMapping("/similarAccount")
    public Result<ListResponse<List<SimilarAccount>>> similarAccount(@Valid @RequestBody SimilarAccountRequest similarAccountRequest) {
        List<SimilarAccount> similarAccounts = accountAnalysisServiceImpl.similarAccount(similarAccountRequest);
        return ResultUtil.success(ListResponse.builder(similarAccounts));
    }


    /**
     * 已添加分析账户
     *
     * @return {@link Result}<{@link ListResponse}<{@link List}<{@link AnalyzedAccount}>>>
     */
    @PostMapping("/analyzedAccounts")
    public Result<ListResponse<List<AnalyzedAccount>>> analyzedAccounts() {
        AnalyzedListRequest analyzedListRequest = new AnalyzedListRequest(null, AnalysisTypeEnum.ACCOUNT.getCode(), AnalysisSourceEnum.telegram.name());
        analyzedListRequest.setUserId(null);
        List<AnalyzedAccount> accountAnalysisMenu = accountAnalysisServiceImpl.analyzedAccount(analyzedListRequest);
        return ResultUtil.success(ListResponse.builder(accountAnalysisMenu));
    }


    /**
     * 加入分析
     *
     * @param joinAnalysisRequest 加入分析请求
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/joinAnalysis")
    public Result<?> joinAnalysis(@Valid @RequestBody JoinAnalysisRequest joinAnalysisRequest) {
        AnalysisDataUser analysisDataUser = new AnalysisDataUser(joinAnalysisRequest.getAnalysisTargetId(), joinAnalysisRequest.getAnalysisTargetName());
        analysisDataUser.setAnalysisType(AnalysisTypeEnum.ACCOUNT.getCode());
        analysisDataUser.setAnalysisSource(AnalysisSourceEnum.telegram.name());
        analysisDataUser.setCreateDate(LocalDateTime.now());
        //analysisDataUser.setUserId(RequestHolderUtil.getUserId());
        accountAnalysisServiceImpl.joinAnalysis(analysisDataUser);
        return ResultUtil.success();
    }


}
